import 'package:flutter/material.dart';
import 'package:flutter_app_custom_chat/chart/utils/ex.dart';

class HistogramChartPaint extends CustomPaint {
  final double width;
  final double height;
  final List<double> data;
  final double maxY;
  final double padding;
  final double indexWidth;
  final double itemWidth;

  HistogramChartPaint(this.width, this.height, this.data, this.maxY,
      this.padding, this.indexWidth, this.itemWidth);

  @override
  Size get size => Size((1 + data.length) * (indexWidth+itemWidth), height);

  @override
  CustomPainter get painter =>
      HistogramPainter(data, maxY, padding, indexWidth, itemWidth);
}

class HistogramPainter extends CustomPainter {
  final List<double> data;
  final double maxY;
  final double padding;
  final double indexWidth;
  final double itemWidth;

  HistogramPainter(
      this.data, this.maxY, this.padding, this.indexWidth, this.itemWidth);

  @override
  void paint(Canvas canvas, Size size) {
    Paint paint = Paint()
      ..color = Colors.blue
      ..isAntiAlias = true
      ..strokeCap = StrokeCap.round
      ..style = PaintingStyle.fill
      ..strokeWidth = 1;
    var point = Offset(padding, size.height - padding);
    var topPoint = Offset(padding, padding);
    for (int i = 1; i <= data.length; i++) {
      var offset = changeRec(
          point, topPoint, indexWidth + itemWidth, padding, i, maxY, data);
      canvas.drawRRect(
          RRect.fromRectAndCorners(
              Rect.fromLTRB(offset.dx - itemWidth / 2, offset.dy,
                  offset.dx + itemWidth / 2, point.dy),
              topLeft: Radius.circular(2),
              topRight: Radius.circular(2)),
          paint);
    }
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return true;
  }
}
